package com.github.willisaway.core.util;

import java.io.IOException;
import java.util.Properties;

/**
 * 读取配置文件的工具
 * 
 * @author LYG
 *
 */
public class ConfigUtil {
    static Properties properties = null;
    static String fileName = "config.properties";// 如果有目录就是"trader/config.properties"

    static {
        load();
    }

    private static void load() {
        try {
            java.io.InputStream is = ConfigUtil.class.getClassLoader().getResourceAsStream(fileName);
            if (is != null) {
                properties = new Properties();
                properties.load(is);
            } else {
                if (!fileName.startsWith("/")) {
                    fileName = "/" + fileName;
                    load();
                } else {
                    throw new IOException("Properties could not be loaded.");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getProperty(String key) {
        if (properties == null) {
            load();
        }
        return properties.getProperty(key);
    }

    public static int getIntProperty(String key) {
        return Integer.parseInt(getProperty(key));
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(ConfigUtil.getProperty("commission"));
    }
}
